﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Middle_869_ReorderedPowerOf2
{
    class MainProjecet
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ReorderedPowerOf2(52));
            Console.ReadKey();
        }
        public static bool ReorderedPowerOf2(int n)
        {
            //反向同过2的次方去用字符串匹配对应的值

            List<string> PowerTableOf2 = new List<string>();
            double value = 0;
            int frequency = 0;
            while (value <= 1000000000)
            {
                value = Math.Pow(2, frequency);
                frequency++;
                if (value <= 1000000000) PowerTableOf2.Add(((long)value).ToString());
            }

            List<string> listForLength = GetListForLength(PowerTableOf2, n.ToString().Length);

            bool isExit = false;
            for (int i = 0; i < listForLength.Count; i++)
            {
                isExit = CheckComposition(listForLength[i], n.ToString());
                if (isExit) break;
            }

            return isExit;
        }

        private static bool CheckComposition(string v1, string v2)
        {
            List<char> chaList = new List<char>(v1);
            foreach (var item in v2)
            {
                if (chaList.Contains(item))
                {
                    chaList.Remove(item);
                }
                else
                    return false;
            }
            return true;
        }

        public static List<string> GetListForLength(List<string> list ,int n)
        {
            List<string> strList = new List<string>();
            foreach (var item in list)
            {
                if (item.Length == n)
                    strList.Add(item);
            }
            return strList;
        }

    }
}
